package com.neothefox.romashka;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Display;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Point;

public class RomashkaPrefs extends Activity implements
OnSeekBarChangeListener, OnCheckedChangeListener, OnSharedPreferenceChangeListener{

	SharedPreferences prefs;
	Editor ed;
	EditText pathBox;
	SeekBar transBar;
	CheckBox userLayout;
	SeekBar yOff;
	SeekBar xOff;
	
	@SuppressLint("CommitPrefEdits")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_romashka_prefs);
		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize(size);
		
		prefs = getSharedPreferences("ROMASHKA_IME", MODE_PRIVATE);
		prefs.registerOnSharedPreferenceChangeListener(this);
		ed = prefs.edit();
		
		xOff = (SeekBar)findViewById(R.id.xoffsetslider);
		yOff = (SeekBar)findViewById(R.id.yoffsetslider);
		userLayout = (CheckBox)findViewById(R.id.checkBox1);
		transBar = (SeekBar)findViewById(R.id.transslider);
		pathBox = (EditText)findViewById(R.id.layoutPath);
		
		yOff.setMax(size.y);
		xOff.setMax(size.x);
		
		yOff.setProgress(prefs.getInt("yOff", 200));
		xOff.setProgress(prefs.getInt("xOff", 200));
		transBar.setProgress(prefs.getInt("trans", 0));
		userLayout.setChecked(prefs.getBoolean("userLayout", false));
		pathBox.setText(prefs.getString("layoutPath", getString(R.string.default_path)));
		
		xOff.setOnSeekBarChangeListener(this);
		yOff.setOnSeekBarChangeListener(this);
		transBar.setOnSeekBarChangeListener(this);
		userLayout.setOnCheckedChangeListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.romashka_prefs, menu);
		return true;
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		switch(seekBar.getId()){
		case R.id.xoffsetslider:
			ed.putInt("xOff", progress);
			break;
		case R.id.yoffsetslider:
			ed.putInt("yOff", progress);
			break;
		case R.id.transslider:
			ed.putInt("trans", progress);
			break;
		default:
			return;
		}
		ed.commit();
		
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch(buttonView.getId()){
		case R.id.checkBox1:
			ed.putBoolean("userLayout", isChecked);
			break;
		default:
			return;
		}
		ed.commit();
	}

	@Override
	protected void onPause() {
		ed.putString("layoutPath",pathBox.getText().toString());
		ed.commit();
		super.onPause();
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		yOff.setProgress(prefs.getInt("yOff", 200));
		xOff.setProgress(prefs.getInt("xOff", 200));
		transBar.setProgress(prefs.getInt("trans", 0));
		userLayout.setChecked(prefs.getBoolean("userLayout", false));
		pathBox.setText(prefs.getString("layoutPath", getString(R.string.default_path)));
		
	}
	
	

}
